package com.kid.keep.controller.impl;

import com.kid.keep.common.SessionStore;
import com.kid.keep.common.UserSubject;
import com.kid.keep.controller.IChildController;
import com.kid.keep.dto.*;
import com.kid.keep.dto.*;
import com.kid.keep.mapper.ICheckItemCovertMapper;
import com.kid.keep.mapper.ICheckReportCovertMapper;
import com.kid.keep.mapper.IChildCovertMapper;
import com.kid.keep.request.AddCheckItemResultRequest;
import com.kid.keep.request.AddChildRequest;
import com.kid.keep.request.CheckItemChildInfoRequest;
import com.kid.keep.request.UpdateChildRequest;
import com.kid.keep.service.ICheckReportService;
import com.kid.keep.service.IChildService;
import com.kid.keep.vo.*;
import com.honeybee.cloud.framework.common.response.ResponseEntity;
import com.kid.keep.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Description 小孩相关请求
 * @Author JOHN
 * @Create 2020-01-03
 * @Since 1.0.0
 */
@Slf4j
@RestController
public class ChildController implements IChildController {

    private IChildCovertMapper childCovertMapper;
    private ICheckItemCovertMapper checkItemCovertMapper;
    private IChildService childService;
    private ICheckReportCovertMapper checkReportCovertMapper;

    @Autowired
    public ChildController(IChildService childService) {
        this.childService = childService;
    }


    @Autowired
    public void setCheckReportCovertMapper(ICheckReportCovertMapper checkReportCovertMapper) {
        this.checkReportCovertMapper = checkReportCovertMapper;
    }

    @Autowired
    public void setChildCovertMapper(IChildCovertMapper childCovertMapper) {
        this.childCovertMapper = childCovertMapper;
    }

    @Autowired
    public void setCheckItemCovertMapper(ICheckItemCovertMapper checkItemCovertMapper) {
        this.checkItemCovertMapper = checkItemCovertMapper;
    }


    @Override
    public ResponseEntity<AddChildVO> addChild(AddChildRequest addUserRequest) {
        UserSubject userSubject = SessionStore.getSessionUser();
        AddChildDTO addChildDTO = childCovertMapper.toDTO(addUserRequest, userSubject.getUserId());
        ChildDTO childDTO = childService.addChild(addChildDTO);
        AddChildVO addChildVO = childCovertMapper.toVO(childDTO);
        return ResponseEntity.ok(addChildVO);
    }

    @Override
    public ResponseEntity<Void> updateChild(Long childId, UpdateChildRequest updateChildRequest) {
        UserSubject userSubject = SessionStore.getSessionUser();
        UpdateChildDTO updateChildDTO = childCovertMapper.toDTO(updateChildRequest, childId);
        childService.updateChild(updateChildDTO,userSubject.getUserId());
        return ResponseEntity.ok();
    }

    @Override
    public ResponseEntity<Void> deleteChild(Long childId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        childService.deleteChild(childId, userSubject.getUserId());
        return ResponseEntity.ok();
    }

    @Override
    public ResponseEntity<ChildVO> getChildDetail(Long childId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        ChildDTO childDTO=childService.getChildByIdAndParentId(childId,userSubject.getUserId());
        return ResponseEntity.ok(childCovertMapper.toChildVO(childDTO));
    }

    @Override
    public ResponseEntity<ChildVO> getChildDetail(CheckItemChildInfoRequest request) {
        UserSubject userSubject = SessionStore.getSessionUser();
        if(Objects.isNull(request)|| Objects.isNull(request.getChildId())){
            ChildDTO childDTO=childService.getNewestChildByParentId(userSubject.getUserId());
            return ResponseEntity.ok(childCovertMapper.toChildVO(childDTO));
        }
        ChildDTO childDTO=childService.getChildByIdAndParentId(request.getChildId(),userSubject.getUserId());
        return ResponseEntity.ok(childCovertMapper.toChildVO(childDTO));
    }


    @Override
    public ResponseEntity<List<ChildVO>> getAllChildByParentId(Long parentId) {
        List<ChildDTO> childDTOList = childService.listChildByParentId(parentId);
        return ResponseEntity.ok(childCovertMapper.toVO(childDTOList));
    }

    @Override
    public ResponseEntity<OpeningCheckItemReportVO> openingCheckItemReport(Long childId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        CheckItemReportDTO checkItemReportDTO = childService.openingCheckItemReport(childId, userSubject.getUserId());
        return ResponseEntity.ok(checkItemCovertMapper.toReportVO(checkItemReportDTO));
    }

    @Override
    public ResponseEntity<OpeningCheckItemReportVO> forcedOpeningCheckItemReport(Long childId) {
        UserSubject userSubject = SessionStore.getSessionUser();
        CheckItemReportDTO checkItemReportDTO = childService.forcedOpeningCheckItemReport(childId, userSubject.getUserId());
        return ResponseEntity.ok(checkItemCovertMapper.toReportVO(checkItemReportDTO));
    }


    @Override
    public ResponseEntity<List<CheckItemReportStatisticsDetailVO>> countCheckReportByDateType(Long childId,Integer type) {
        UserSubject userSubject = SessionStore.getSessionUser();
        List<CheckItemReportDTO> checkItemReportList = childService.countCheckReportByDateTypeAndChildId(childId,type, userSubject.getUserId());
        return ResponseEntity.ok(checkReportCovertMapper.toStatisticsVO(checkItemReportList));
    }



}
